﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class ConfigBLL
  {
    public bool isHasValue()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        if (data.Configs.ToList().Count > 0) return true;
        else return false;
      }
    }

    public Config GetDefautl()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Configs.FirstOrDefault();
      }
    }
    public void UpdateContact(string strContact,string strContact_En)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        if (!isHasValue())
        {
          Config obj = new Config();
          obj.Id = 1;
          obj.Contact = strContact;
          obj.Contact_En = strContact_En;

          data.Configs.InsertOnSubmit(obj);
        }
        else
        {
          Config obj = data.Configs.FirstOrDefault();
          obj.Contact = strContact;
          obj.Contact_En = strContact_En;
        }

        data.SubmitChanges();
      }
    }

    public void Update(Config config)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            if (!isHasValue())
            {
                Config obj = new Config();
                obj.Id = 1;
                obj.Contact = config.Contact;
                obj.Contact_En = config.Contact_En;
                obj.Facebook = config.Facebook;
                obj.Footer = config.Footer;
                obj.Footer_En = config.Footer_En;
                obj.Hotline = config.Hotline;
                obj.Skype = config.Skype;
                obj.Weekday1 = config.Weekday1;
                obj.Weekday1_Value = config.Weekday1_Value;
                obj.Weekday2 = config.Weekday2;
                obj.Weekday2_Value = config.Weekday2_Value;
                obj.Yahoo = config.Yahoo;
                

                data.Configs.InsertOnSubmit(obj);
            }
            else
            {
                Config obj = data.Configs.FirstOrDefault();
                obj.Contact = config.Contact;
                obj.Contact_En = config.Contact_En;
                obj.Facebook = config.Facebook;
                obj.Footer = config.Footer;
                obj.Footer_En = config.Footer_En;
                obj.Hotline = config.Hotline;
                obj.Skype = config.Skype;
                obj.Weekday1 = config.Weekday1;
                obj.Weekday1_Value = config.Weekday1_Value;
                obj.Weekday2 = config.Weekday2;
                obj.Weekday2_Value = config.Weekday2_Value;
                obj.Yahoo = config.Yahoo;
            }

            data.SubmitChanges();
        }
    }

    public void UpdateFooter(string strFooter, string strFooter_En)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        if (!isHasValue())
        {
          Config obj = new Config();
          obj.Id = 1;
          obj.Footer = strFooter;
          obj.Footer_En = strFooter_En;
          data.Configs.InsertOnSubmit(obj);
        }
        else
        {
          Config obj = data.Configs.FirstOrDefault();
          obj.Footer = strFooter;
          obj.Footer_En = strFooter_En;
        }

        data.SubmitChanges();
      }
    }

    public void UpdateSEO(string description, string keyword, string copyright, string author)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))      
      {
        if (!isHasValue())
        {
          Config obj = new Config();
          obj.Id = 1;
          obj.Description = description;
          obj.Keywords = keyword;
          obj.Copyright = copyright;
          obj.Author = author;
        }
        else 
        {
          Config obj = data.Configs.FirstOrDefault();
          obj.Description = description;
          obj.Keywords = keyword;
          obj.Copyright = copyright;
          obj.Author = author;
        }
        data.SubmitChanges();
      }
    }
  }
}
